package cn.besbing.server.controllers.pages;

import cn.besbing.client.enums.BaseResponse;
import cn.besbing.model.entities.primary.*;
import cn.besbing.server.service.limsweb.LimsWebActionsService;
import cn.besbing.server.service.primary.*;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.ui.Model;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("lims")
@Controller
public class LimsPageControllers {

    @Autowired
    PrimaryListEntryServiceImpl listEntryService;

    @Autowired
    PrimaryOrgOrgsServiceImpl orgOrgsService;

    @Autowired
    PrimaryDlSampleTypeServiceImpl sampleTypeService;

    @Autowired
    PrimaryDlActionImpl primaryDlAction;

    @Autowired
    RejectProcessDetailServiceImpl detailService;

    @Autowired
    RejectProcessBlobServiceImpl blobService;

    @Autowired
    PrimarySmuserServiceImpl smuserService;

    @Autowired
    ReportRejectInfoServiceImpl rejectInfoService;

    @Autowired
    LimsWebActionsService webActionsService;




    @Autowired
    PrimaryCustomServiceImpl customService;

    @GetMapping("projectmodify")
    public String projectmodify(){
        return "pages/lims/serviceforself/projectmodify";
    }

    @GetMapping("confirmcharge")
    public String confirmcharge(){
        return "pages/lims/serviceforself/confirmcharge";
    }

    @GetMapping("analysismanage")
    public String analysismanage(){
        return "pages/lims/manage/analysismanage";
    }


    /**
     * 跳转时直接带入委托单初始值
     * @return
     */
    @GetMapping("newproject")
    public String newproject(Model model){
        /**
         * 开始获取所有预设的参照值
         */
        model.addAttribute("projType",listEntryService.getListValueByListName("C_APPLY_TYPE"));
        model.addAttribute("reportType",listEntryService.getListValueByListName("C_COA_FORMAT"));
        model.addAttribute("reportLang",listEntryService.getListValueByListName("C_COA_LANGUAGE"));
        model.addAttribute("sampleAfterTestProcess",listEntryService.getListValueByListName("C_RATAIN_HANDLE"));
        model.addAttribute("productProperty",listEntryService.getListValueByListName("产品属性"));
        model.addAttribute("requiredType",listEntryService.getListValueByListName("客户要求-需求类型"));
        model.addAttribute("testType",listEntryService.getListValueByListName("客户要求-测试类型"));
        model.addAttribute("testNatrueType",listEntryService.getListValueByListName("检验性质类型"));
        model.addAttribute("productIdentificationType",listEntryService.getListValueByListName("产品鉴定类型"));
        model.addAttribute("safetyCertificationType",listEntryService.getListValueByListName("安全认证类型"));
        model.addAttribute("orgs",orgOrgsService.getAllorg());
        model.addAttribute("myorg",orgOrgsService.getOrgByUsercode(SecurityUtils.getSubject().getSession().getAttribute("usercode").toString()));
        return "pages/lims/limsweb/createproject";
    }


    @GetMapping("samplereceive")
    @RequiresPermissions(value = {"lims:admin","lims:samplereceive"},logical = Logical.OR)
    public String samplereceive(){
        /*if(SecurityUtils.getSubject().isPermitted("lims:admin") || SecurityUtils.getSubject().isPermitted("lims:samplereceive")){
            return "pages/lims/limsweb/samplereceive";
        }else {
            return "没权限";
        }*/
        return "pages/lims/limsweb/samplereceive";
    }

    @GetMapping("clearLocationStorage")
    public String clearLocationStorage(){
        return "pages/lims/limsweb/sample/clearlocationstorage";
    }


    /**
     * 样品出入库设置
     * @param model
     * @return
     */
    @GetMapping("sampleType")
    public String sampleType(Model model){
        List<DlSampleType> list = sampleTypeService.getAllSampleType();
        model.addAttribute("sampleType",list);
        return "pages/lims/limsweb/manage/samplewarehousing";
    }


    /**
     * nc模板传输预加载数据
     * @param model
     * @return
     */
    @GetMapping("ncpreview")
    public String ncPreviewLoad(Model model){
        List<String> listProduct = customService.selectAsList("select pu.description || ':' || pu.version des " +
                "from product pu where  pu.active = 'T' order by pu.description ");
        model.addAttribute("productList",listProduct);
        return "pages/lims/serviceforself/ncpreview";
    }



    /**
     * 日志动作设置
     * @param
     * @return
     */
    @GetMapping("logSetting")
    public String logSetting(){
        /*List<String> listProduct = customService.selectAsList("select pu.description || ':' || pu.version des " +
                "from product pu where  pu.active = 'T' order by pu.description ");
        model.addAttribute("productList",listProduct);*/
        //List<DlActionTable> listAction = primaryDlAction.getAllLogAction();
        //model.addAttribute("actionList",listAction);
        return "pages/lims/manage/logSetting";
    }

    /**
     * 工程师
     */
    @GetMapping("engieneer")
    public String engineer(){
        return "pages/lims/limsweb/engineer";
    }


    @RequestMapping(value = "/taskProcess",method = RequestMethod.GET)
    public String taskProcess(){
        return "pages/lims/limsweb/manage/allTaskProgress";
    }


    /**
     * 委托单预览界面跳转
     * @return
     */
    @RequestMapping(value = "ProjectPreview",method = RequestMethod.GET)
    public String ProjectPreview(){
        return "pages/lims/limsweb/manage/ProjectPreview";
    }

    /**
     * 任务管理页面
     * @return
     */
    @RequestMapping(value = "/task",method = RequestMethod.GET)
    public String task(){
        return "pages/lims/limsweb/task/task";
    }

    /**
     * 报告重签
     * @return
     */
    @RequestMapping(value = "/reportReSign",method = RequestMethod.GET)
    public String ReportReSign(){
        return "pages/lims/manage/ReportReSign";
    }


    @RequestMapping(value = "rejectManage",method = RequestMethod.GET)
    public String rejectManage(){
        return "pages/lims/manage/rejectManage";
    }

    @RequestMapping(value = "sendRejectToHuman",method = RequestMethod.GET)
    public String sendRejectToHuman(){
        return "pages/lims/manage/sendRejectToHuman";
    }

    @RequestMapping(value = "rejectProcessPage",method = RequestMethod.GET)
    public String rejectProcessPage(){
        return "pages/lims/manage/rejectProcess";
    }

    /**
     * 转驳回明细页
     * @param model
     * @param pk
     * @return
     */
    @GetMapping("rejectDetailPage")
    public String rejectDetailPage(Model model,String pk){
        BaseResponse baseResponse = webActionsService.createRejectDetailPageModel(model,pk);
        if (baseResponse.getCode() == 200)
            model = (Model) baseResponse.getData();
        return "pages/lims/manage/rejectDetailPage";
    }

}
